package com.jifeng.api.exception.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class Auth2ResponseExceptionTranslator implements WebResponseExceptionTranslator {

    @Override
    public ResponseEntity translate(Exception e) {
        log.error("Auth2异常", e);
        Throwable throwable = e.getCause();
        if (throwable instanceof InvalidTokenException) {
            log.info("token失效:{}", throwable);
            Map<String, Object> errMap = new HashMap<>();
            errMap.put("code", 422);
            errMap.put("msg", "token已失效");
            return new ResponseEntity(errMap, HttpStatus.OK);
        }
        return new ResponseEntity(null, HttpStatus.METHOD_NOT_ALLOWED);
    }
}
